#include <stdio.h>
#include <stdlib.h>

#include "../Utils/Utils.h"
#include "../LocalStack/LocalStack.h"

#include "NumList.h"

#include "NumListIO.h"

NumList* ReadNumList(const char* fileName)
{
	FILE* inpFile;
	NumList* ret;
	
	START();
	
	if (NULL == (inpFile = fopen(fileName, "r")))
		return NULL;
	
	SAFED(inpFile, FileCloser);
	SAFED(ret = ScanNumList(inpFile), DeleteNumList);
	Assert(fclose(inpFile) == EOF);

	RETURN(ret);
}

NumList* ScanNumList(FILE* inpFile)
{	
	NumList *retListIter = NULL, *retList;
	
	START();
	
	Assert(inpFile != NULL);
	
	SAFED(retList = NewNumList(), DeleteNumList);
	retListIter = retList;
	
	while(!feof(inpFile))
	{
		Assert((fscanf(inpFile, "%d", &(retListIter->value)) == 1));
		retListIter = (retListIter->next = NewNumList());
	}
	
	SetNumListSizes(retList);
	
	RETURN(retList);
}

bool PrintNumList(const NumList* numList)
{
	for (; numList->next != NULL; numList = numList->next)
	{
		if (0 > printf("%d ", numList->value))
			return FALSE;
	}

	return TRUE;
}
